package com.ocom.accesservice.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.PersonnelAreaService;
import com.ocom.accesservice.service.PersonnelSumService;
import com.ocom.accesservice.vo.PersonnelSumVo;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.PersonnelAreaRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.access.PersonnelAreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PersonnelController {

    @Autowired
    private PersonnelSumService personnelSumService;

    @Autowired
    private PersonnelAreaService personnelAreaService;

    @PostMapping(AccessServiceUrl.URL_GetStudentSumBySchool)
    public Result getStudentSumBySchool(Long companyId){
        List<PersonnelSumVo> personnelSumVo=personnelSumService.selectPersonnelSum(companyId);
        if (personnelSumVo.size()<=0){
            return ResultUtil.error(ResultEnum.Err_1001);
        }
        return ResultUtil.success(personnelSumVo);
    }

    //区域人员权限列表
    @PostMapping(AccessServiceUrl.URL_getAreaAccessList)
    public Result getAreaAccessList(@RequestBody @Validated PersonnelAreaRequest personnelAreaRequest){
        Map<String,Object> map=new HashMap<>();
        if (personnelAreaRequest.getAccess()==null){
            personnelAreaRequest.setAccess(-2);
        }
        List<PersonnelAreaVo> list=null;
        if (personnelAreaRequest.getAccess()==-1 || personnelAreaRequest.getAccess()>0){
            list=personnelAreaService.getAreaAccessList(personnelAreaRequest.getComId(),personnelAreaRequest);
        }else {
            list=personnelAreaService.getAreaAccessListBu(personnelAreaRequest.getComId(),personnelAreaRequest);
        }
        if(list==null || list.size()==0){
            list=new ArrayList<>();
        }
        map.put("total", 0);
        if (personnelAreaRequest.getPage()!=-1 && personnelAreaRequest.getSize()!=-1){
            PageInfo<PersonnelAreaVo> pageInfo=new PageInfo<>(list);
            map.put("total", pageInfo.getTotal());
            map.put("accessList", pageInfo.getList());
            if(pageInfo.getList() == null || pageInfo.getList().size()==0){
                map.put("accessList", new ArrayList<>());
            }
        }else {
            map.put("accessList", list);
        }
        return ResultUtil.success(map);
    }
}
